import type { ComponentConstructorOptions } from 'svelte';
import Dialog from './Dialog.svelte';

type DialogOptions = {
	title: string;
	content: string;
	icon: string;
	danger: boolean;
};

export const createDialog = (
	title: string,
	message: string,
	callback: () => void = () => void 0,
	icon: string = 'info',
	danger: boolean = false
) => {
	const options: ComponentConstructorOptions<DialogOptions> = {
		target: document.querySelector('.app')!,
		props: {
			title: title,
			content: message,
			icon,
			danger
		},
		// hydrate: true,
		context: new Map([['title', '123']])
	};

	const dialogDom = new Dialog(
		options as unknown as ComponentConstructorOptions<Record<string, never>>
	);

	dialogDom.$on('cancel', () => {
		dialogDom.$destroy();
	});

	dialogDom.$on('confirm', () => {
		if (callback) callback();
		dialogDom.$destroy();
	}); // TODO: fix this

	return dialogDom;
};
